package com.tywj.panda.customer.common.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout

import com.github.florent37.viewanimator.ViewAnimator
import com.tywj.panda.customer.common.R
import com.tywj.lib.businessweak.view.IBaseView
import com.tywj.lib.core.common.extension.applyScheduler
import com.tywj.lib.core.common.extension.invisible
import com.tywj.lib.core.common.extension.safeDisposable
import com.tywj.lib.core.common.extension.visible
import com.tywj.lib.core.common.util.AnimationUtil
import com.tywj.lib.core.common.util.LogUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.lifecycle.extension.attachToLifecycle
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.common_include_loading.view.*
import java.util.concurrent.TimeUnit

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/05/08
 *     desc   : 加载中动画控件，支持动画延迟显示，也就是说对于加载中动画显示时间小于500毫秒的场景不显示
 *     version: 0.6
 * </pre>
 */
class LoadingView : FrameLayout {

    companion object {
        const val DELAY_DURATION = 500L
    }

    /** 旋转动画 */
    private var rotateAnim: ViewAnimator? = null

    constructor(context: Context) : this(context, null, 0)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        LayoutInflater.from(context).inflate(R.layout.common_include_loading, this, true)

        invisible(this)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        // 这里直接写死宽高
        val dimen = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_33)
        val measureSpec = MeasureSpec.makeMeasureSpec(dimen, MeasureSpec.EXACTLY)
        super.onMeasure(measureSpec, measureSpec)
    }

    private var showLoadingAnimDisposable: Disposable? = null

    fun startAnimationWithDelay(baseView: IBaseView) {
        if (rotateAnim != null) {
            return
        }
        if (showLoadingAnimDisposable != null) {
            return
        }

        showLoadingAnimDisposable = Observable.just("")
            .delay(DELAY_DURATION, TimeUnit.MILLISECONDS)
            .applyScheduler()
            .subscribe({
                visible(this)
                rotateAnim = AnimationUtil.animate(ivLoading)
                    .duration(300)
                    .rotation(0f, 360f)
                    .repeatCount(-1)
                    .interpolator(LinearInterpolator())
                    .start()
            }, {
                LogUtil.printStackTrace(it)
            })
        showLoadingAnimDisposable?.attachToLifecycle(baseView.getLifecycleOwner())
    }

    fun startAnimation() {
        if (rotateAnim != null) {
            return
        }

        visible(this)
        rotateAnim = AnimationUtil.animate(ivLoading)
            .duration(300)
            .rotation(0f, 360f)
            .repeatCount(-1)
            .interpolator(LinearInterpolator())
            .start()
    }

    fun cancelAnimation() {
        showLoadingAnimDisposable?.safeDisposable()
        showLoadingAnimDisposable = null
        rotateAnim?.cancel()
        rotateAnim = null
        invisible(this)
    }
}